//
//  ShmMusicXmlModel.swift
//  ParsingCC
//
//  Created by ins-yangming on 2023/1/6.
//

import UIKit
//import XMLMapper


// MARK: - musuc xml 总类
class ShmMusicXmlModel: NSObject {
    //根节点名称
    var nodeName = "";
    //版本
    var version = "0.0";
    //歌曲名称
    var musicName = "";
    
    var identification : ShmIdentificationModel?;
    
    var partList : ShmPartListModel?;
    
    var parts = [ShmPartModel]();

    func getXmlString(fileName : String) {
        if let root = GDataXMLNode.element(withName: self.nodeName) {
            let attribute = GDataXMLNode.attribute(withName: "version", stringValue: self.version) as! GDataXMLNode;
            root.addAttribute(attribute);
            
            
            let work = GDataXMLNode.element(withName: "work")!;
            let workList = GDataXMLNode.element(withName: "work-title" , stringValue: self.musicName)!;
            work.addChild(workList)
            root.addChild(work);
            
            if let identification = self.identification?.getXmlElement() {
                root.addChild(identification);
            }
            
            if let identification = self.partList?.getXmlElement() {
                root.addChild(identification);
            }
            
            for part in self.parts {
                root.addChild(part.getXmlElement());
            }
            
            // 生成xml文件内容
            let xmlDoc = GDataXMLDocument(rootElement: root);

            if let data = xmlDoc?.xmlData() {
                let xmlString = String(data: data, encoding: String.Encoding.utf8);
                let xmlPath = NSHomeDirectory() + "/Documents/\(fileName).xml";
                try? xmlString?.write(toFile: xmlPath, atomically: true, encoding: String.Encoding.utf8);
            }
        }
    }
    
}


